#include <windows.h>
#include <cstring>
#include "Retire.h"

LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

HWND hWnd;
HWND hwndAnnualLabel;
HWND hwndAnnualText;
HWND hwndYieldLabel;
HWND hwndYieldText;
HWND hwndCurrentAgeLabel;
HWND hwndCurrentAgeText;
HWND hwndRetAgeLabel;
HWND hwndRetAgeText;
HWND hwndInflationLabel;
HWND hwndInflationText;
HWND hwndFutureValueButton;
HWND hwndFutureValueText;
HWND hwndPresentValueButton;
HWND hwndPresentValueText;
HWND hwndTotalInterestButton;
HWND hwndTotalInterestText;
RetirementPlanner savings;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
	LPSTR lpCmdLine, int nCmdShow) {

	WNDCLASS wc;

	wc.lpszClassName = "RetirementClass";
	wc.lpfnWndProc = MainWndProc;
	wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)( COLOR_BACKGROUND );
	wc.lpszMenuName = "";
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	RegisterClass( &wc );

	hWnd = CreateWindow(
		"RetirementClass", "Retirement Planner",
		WS_OVERLAPPEDWINDOW, 
		CW_USEDEFAULT,
		NULL,
		490,
		240,
		NULL,
		NULL,
		hInstance,
		NULL);

	hwndAnnualLabel = CreateWindow( 
		"Static", 
		" Annual Contribution", 
		WS_VISIBLE | WS_CHILD | SS_SUNKEN ,
		10,
		10, 
		250, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndAnnualText = CreateWindow( 
		"EDIT",
		"0", 
		WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_LEFT, 
		275,
		10, 
		200, 
		20, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	hwndYieldLabel = CreateWindow( 
		"Static", 
		" Annual Yield (percent)", 
		WS_VISIBLE | WS_CHILD | SS_SUNKEN ,
		10,
		35, 
		250, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndYieldText = CreateWindow( 
		"EDIT",
		"0", 
		WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_LEFT ,
		275,
		35, 
		200, 
		20, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	hwndCurrentAgeLabel = CreateWindow( 
		"Static", 
		" Current Age", 
		WS_VISIBLE | WS_CHILD | SS_SUNKEN ,
		10,
		60, 
		250, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndCurrentAgeText = CreateWindow( 
		"EDIT",
		"0", 
		WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_LEFT ,
		275,
		60, 
		200, 
		20, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	hwndRetAgeLabel = CreateWindow( 
		"Static", 
		" Retirement Age", 
		WS_VISIBLE | WS_CHILD | SS_SUNKEN ,
		10,
		85, 
		250, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndRetAgeText = CreateWindow( 
		"EDIT",
		"0", 
		WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_LEFT ,
		275,
		85, 
		200, 
		20, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	hwndInflationLabel = CreateWindow( 
		"Static", 
		" Inflation (percent)", 
		WS_VISIBLE | WS_CHILD | SS_SUNKEN ,
		10,
		110, 
		250, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndInflationText = CreateWindow( 
		"EDIT",
		"0", 
		WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | ES_LEFT ,
		275,
		110, 
		200, 
		20, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	hwndFutureValueButton = CreateWindow( 
		"Button", 
		" Total Future Value", 
		WS_VISIBLE | WS_CHILD  | WS_TABSTOP,
		10,
		135, 
		250, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndFutureValueText = CreateWindow( 
		"STATIC",
		" CALCULATED", 
		WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT ,
		275,
		135, 
		200, 
		20, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	hwndPresentValueButton = CreateWindow( 
		"Button", 
		" Total Present Value", 
		WS_VISIBLE | WS_CHILD  | WS_TABSTOP,
		10,
		160, 
		250, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndPresentValueText = CreateWindow( 
		"STATIC",
		" CALCULATED", 
		WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT ,
		275,
		160, 
		200, 
		20, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 
	

	hwndTotalInterestButton = CreateWindow( 
		"Button", 
		" Total Interest Earned", 
		WS_VISIBLE | WS_CHILD  | WS_TABSTOP,
		10,
		185, 
		250, 
		20, 
		hWnd,
		NULL, 
		hInstance, 
		NULL); 

	hwndTotalInterestText = CreateWindow( 
		"STATIC",
		" CALCULATED", 
		WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT ,
		275,
		185, 
		200, 
		20, 
		hWnd, 
		NULL, 
		hInstance, 
		NULL); 

	ShowWindow(hWnd, nCmdShow);

	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0 )) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int) msg.wParam;
}


LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, 
	WPARAM wParam, LPARAM lParam) {
	HWND hwndCtl = (HWND) lParam;
	char szContributeText[10] = "";
	char szYield[10] = "";
	char szCurrentAgeText[5] = "";
	char szRetAgeText[5] = "";
	char szInflationText[5] = "";
	double dContribute = 0;
	double dYield = 0;
	double dInflation = 0;
	int iCurrentAge = 0;
	int iRetAge = 0;
	double dReturnValue;
	char szResult[25];
	switch (msg) {
		case WM_COMMAND:
			switch (wParam) {
				case BN_CLICKED:
					if (hwndCtl == hwndFutureValueButton || 
                        hwndCtl == hwndPresentValueButton) 
                    {
						GetWindowText(hwndAnnualText, szContributeText, 10);
						dContribute = atof(szContributeText);
						GetWindowText(hwndYieldText, szYield, 10);
						dYield = atof(szYield);
						GetWindowText(hwndCurrentAgeText, szCurrentAgeText, 5);
						iCurrentAge = atoi(szCurrentAgeText);
						GetWindowText(hwndRetAgeText, szRetAgeText, 5);
						iRetAge = atoi(szRetAgeText);
						GetWindowText(hwndInflationText, szInflationText, 10);
						dInflation = atof(szInflationText);
						savings.setContribution(dContribute);
						savings.setInterestRate(dYield);
						savings.setCurAge(iCurrentAge);
						savings.setRetireAge(iRetAge);
						savings.setInflation(dInflation);
						if (hwndCtl == hwndFutureValueButton) {
							dReturnValue = savings.calcFutureValue();
							_gcvt(dReturnValue, 10, szResult);
							SetWindowText(hwndFutureValueText, szResult);
						}
						else if (hwndCtl == hwndPresentValueButton) {
							GetWindowText(hwndInflationText, szInflationText, 
                                          5);
							dInflation = atof(szInflationText);
							dReturnValue = savings.calcPresentValue();
							_gcvt(dReturnValue, 10, szResult);
							SetWindowText(hwndPresentValueText, szResult);
						}
					}
					else if (hwndCtl == hwndTotalInterestButton) {
						dReturnValue = savings.getInterestEarned();
						_gcvt(dReturnValue, 10, szResult);
						SetWindowText(hwndTotalInterestText, szResult);
					}
			}
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
		default:
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}
